/// Creator:      gaoruishan
/// CreatDate:    2025-06-14
/// Description:  检验
Class Nur.MNISV3.Service.Lab Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 检验列表
/// w ##class(Nur.MNISV3.Service.Lab).GetLabOrderDataV4("","2025-06-11","2025-06-13","3","20665","8","{""single"":"""",""multiple"":""1""}")
ClassMethod GetLabOrderDataV4(episodeId, fromDate, toDate, wardId, userId, locId, extras)
{
    n (episodeId,fromDate,toDate,wardId,userId,locId, extras)
    s $zt="errGetLabOrderDataV4"
    s ^GRS("GetLabOrderDataV4")=$lb(episodeId,fromDate,toDate,wardId,userId,locId, extras)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s fromDate=##class(websys.Conversions).DateHtmlToLogical(fromDate)
    s toDate=##class(websys.Conversions).DateHtmlToLogical(toDate)
    s patInfoList=##class(%ListOfDataTypes).%New()
    i episodeId'="" { //单患者
       s patinfo=##class(%ArrayOfDataTypes).%New()
       s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
       s labOrderData=##class(Nur.MNISV3.Service.Lab).SelectLISItemListByDate(episodeId,fromDate,toDate,"")
       d patinfo.SetAt(labOrderData,"labOrderData")
       d patInfoList.Insert(patinfo)
    }else {
          //调用纯净版患者列表，
        s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
        d patListObj.Execute(wardId,userId,"Ward",locId)
        
        while(patListObj.Next()){
           s patinfo= ##class(Nur.MNISV3.Common.Utils).ResultSet(patListObj)
           s episodeId=patListObj.GetDataByName("episodeId")
           s labOrderData=##class(Nur.MNISV3.Service.Lab).SelectLISItemListByDate(episodeId,fromDate,toDate,"")
           d patinfo.SetAt(labOrderData,"labOrderData")
           //基本患者信息
           s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
           d patInfoList.Insert(patinfo)
        }
    }
    
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetLabOrderDataV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// 调用LIS接口-获取检验列表
ClassMethod SelectLISItemListByDate(EpisodeID, Begdate, EndDate, FExistReport = "")
{
    n (EpisodeID , Begdate , EndDate , FExistReport)
    s labOrderData=##class(%ListOfDataTypes).%New()
    s LisOrdres=##class(%ResultSet).%New("web.DHCENS.STBLL.Method.PostReportInfo:SelectLISItemListByDate")
    d LisOrdres.Execute(EpisodeID,Begdate,EndDate,FExistReport)
    while LisOrdres.Next()
    {
       s LisOrdData=##class(Nur.MNISV3.Common.Utils).ResultSet(LisOrdres)
       s oeordId=LisOrdres.GetDataByName("OEordItemRowID")
       //详情
       s Report=##class(Nur.MNISV3.Service.Lab).SelectLISReportByOeordID(oeordId)
       d LisOrdData.SetAt(Report,"Report")
       d labOrderData.Insert(LisOrdData)
    } 
    q labOrderData
}

/// 调用LIS接口-获取报告详情
ClassMethod SelectLISReportByOeordID(oeoreId)
{
    n (oeoreId)
    s resultDetail=##class(%ListOfDataTypes).%New()
    ///检验组接口
    for len=1:1:$L(oeoreId,",")
    {
        s coeoreId=$P(oeoreId,",",len)
        s resultInfo=##class(%ResultSet).%New("web.DHCENS.STBLL.Method.PostReportInfo:SelectLISReportByOeordID")
        d resultInfo.Execute(coeoreId)
        while resultInfo.Next()
        {
            s result=##class(Nur.MNISV3.Common.Utils).ResultSet(resultInfo)
            d resultDetail.Insert(result)
        }
    }
    q resultDetail
}

}
